/*
 *  (C) Copyright 2008 Insula Tecnologia da Informacao Ltda.
 * 
 *  This file is part of Vulcano.
 *
 *  Vulcano is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Vulcano is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Vulcano.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.openinsula.vulcano.orm.command;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.openinsula.vulcano.core.command.Command;
import org.openinsula.vulcano.orm.command.transaction.InitializeCallback;
import org.openinsula.vulcano.orm.dao.Dao;
import org.openinsula.vulcano.orm.dao.query.DaoQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Eduardo R Danielli
 *
 */
@Component
public class DaoCommandFactory {

	@Autowired
	private Dao dao;
	
	public <T> TransactionCommand<T> createFindByIdCommand(final Class<T> entityClass, final Object id) {
		return new TransactionCommand<T>() {
			private static final long serialVersionUID = 1L;
			
			@Transactional(readOnly = true)
			@Override
			public T execute() {
				return initialize(dao.findById(entityClass, id));
			}
			
		};
	}
	
	public <T> TransactionCommand<List<T>> createListAllCommand(final Class<T> entityClass) {
		return new TransactionCommand<List<T>>() {
			private static final long serialVersionUID = 1L;
			
			@Transactional(readOnly = true)
			@Override
			public List<T> execute() {
				return initialize(dao.listAll(entityClass));
			}
		};
	}
	
	public <T> TransactionCommand<List<T>> createFindCommand(final String queryString, final Object... values) {
		return new TransactionCommand<List<T>>() {
			private static final long serialVersionUID = 1L;
			
			@Transactional(readOnly = true)
			@Override
			public List<T> execute() {
				return initialize(dao.<T>find(queryString, values));
			}
		};
	}
	
	public <T> TransactionCommand<List<T>> createFindCommand(final DaoQuery daoQuery) {
		return new TransactionCommand<List<T>>() {
			private static final long serialVersionUID = 1L;
			
			@Transactional(readOnly = true)
			@Override
			public List<T> execute() {
				return initialize(dao.<T>find(daoQuery));
			}
		};
	}
	
	public <T> TransactionCommand<T> createFindUniqueCommand(final String queryString, final Object... values) {
		return new TransactionCommand<T>() {
			private static final long serialVersionUID = 1L;
			
			@Transactional(readOnly = true)
			@Override
			public T execute() {
				return initialize(dao.<T>findUnique(queryString, values));
			}
		};
	}
	
	public <T> TransactionCommand<T> createFindUniqueCommand(final DaoQuery daoQuery) {
		return new TransactionCommand<T>() {
			private static final long serialVersionUID = 1L;
			
			@Transactional(readOnly = true)
			@Override
			public T execute() {
				return initialize(dao.<T>findUnique(daoQuery));
			}
		};
	}
	
	public <T> Command<Integer> createSaveCommand(final T ... entities) {
		return new TransactionCommand<Integer>() {
			private static final long serialVersionUID = 1L;

			@Override
			public Integer execute() {
				dao.saveAll(Arrays.asList(entities));
				return entities.length;
			}
			
		};
	}
	
	public <T> Command<Integer> createSaveCollectionCommand(final Collection<T> entities) {
		return new TransactionCommand<Integer>() {
			private static final long serialVersionUID = 1L;

			@Override
			public Integer execute() {
				dao.saveAll(entities);
				return entities.size();
			}
			
		};
	}
	
	public <T> Command<T> createMergeCommand(final T entity) {
		return new TransactionCommand<T>() {
			private static final long serialVersionUID = 1L;

			@Override
			public T execute() {
				return dao.merge(entity);
			}
			
		};
	}
	
	public <T> Command<Integer> createDeleteCommand(final T ... entities) {
		return new TransactionCommand<Integer>() {
			private static final long serialVersionUID = 1L;

			@Override
			public Integer execute() {
				dao.deleteAll(Arrays.asList(entities));
				return entities.length;
			}
			
		};
	}
	
	public <T> Command<Integer> createDeleteCollectionCommand(final Collection<T> entities) {
		return new TransactionCommand<Integer>() {
			private static final long serialVersionUID = 1L;

			@Override
			public Integer execute() {
				dao.deleteAll(entities);
				return entities.size();
			}
			
		};
	}
	
	public Command<Integer> createDeleteTypeCommand(final Class<?> entityClass) {
		return new TransactionCommand<Integer>() {
			private static final long serialVersionUID = 1L;

			@Override
			public Integer execute() {
				return dao.deleteAll(entityClass);
			}
			
		};
	}
	
	public Command<Integer> createUpdateCommand(final String queryString, final Object... values) {
		return new TransactionCommand<Integer>() {
			private static final long serialVersionUID = 1L;
			
			@Override
			public Integer execute() {
				return dao.update(queryString, values);
			}
		};
	}
	
	public <T> TransactionCommand<T> createRefreshCommand(final T entity) {
		return new TransactionCommand<T>() {
			private static final long serialVersionUID = 1L;
			
			@Transactional(readOnly = true)
			@Override
			public T execute() {
				dao.refresh(entity);
				
				return initialize(entity);
			}
		};
	}
	
	public <T> TransactionCommand<T> createReattachCommand(final T entity) {
		return new TransactionCommand<T>() {
			private static final long serialVersionUID = 1L;
			
			@Override
			public T execute() {
				dao.reattach(entity);
				return initialize(entity);
			}
		};
	}
	
	public abstract class TransactionCommand<T> implements Command<T> {

		private InitializeCallback<T> initializeCallback;
		
		public void setInitializeCallback(final InitializeCallback<T> initializeCallback) {
			this.initializeCallback = initializeCallback;
		}
		
		public final T initialize(final T obj) {
			if (initializeCallback != null && obj != null) {
				initializeCallback.initialize(obj);
			}
			
			return obj;
		}

	}
	
}